import { useEffect, useState } from 'react'
import '@/App.css'

function App() {
  const [count, setCount] = useState(10)
  // useEffect(()=>{
  //   const timer=setInterval(()=>{
  //     setCount(prev=>{
  //       if(prev<=0){
  //         clearInterval(timer)
  //         return 0
  //       }
  //       return prev-1
  //     })
  //   },1000)

  //   return ()=>clearInterval(timer)
  // },[])
  useEffect(()=>{
    const timer=setInterval(()=>{
      setCount(prev=>{
         if(timer<=0){
        clearInterval(timer)
        return 0
      }
      return prev-1
      })
    },1000)
    return ()=> clearInterval(timer)
  },[])


  return (
   <div>
    <h1>倒计时</h1>
    <div>
     {count<=0?'倒计时结束!':`0天 0时 0分 ${count}秒`}
    </div>


   </div>
  )
}

export default App
